<template>
  <div>
      <a-modal v-model="visible" :title="$i18n('collaboration.forward.page.label4')" @ok="handleOk" @cancel="revert" :destroyOnClose="true">
      <Track  width="424"
              style="height: 70px"
              :disable="disable"
              :isTrack.sync="trackSet.isTrack"
              :isTrackAll.sync="trackSet.isTrackAll"
              :trackMembers.sync="trackSet.trackMembers"
              :trackMemberNames.sync="trackSet.trackMemberNames" />
      </a-modal>    
  </div>
</template>
<script>
import Track from "@/components/track";
import emitter from "@/utils/emitter";
import {mapState} from "pinia";
import {useSummaryCommentStore} from "@/store/modules/summary/comment";
import {useBaseInfoStore} from "@/store/modules/base-info";
import globalMethods from "@/plugins/methods";

export default {
  name:'TrackModal',
  components: {Track},
  data() {
    return {
      visible: false,
      disable:false,
    };
  },
  computed:{
    ...mapState(useSummaryCommentStore, ['trackSet']),
    ...mapState(useBaseInfoStore, ['businessData']),
  },
  created() {
    this.oldTrackType=this.getCurrentTrackType();
    //前一次的保存结果,取消之后要还原
    this.preTack = {
      ...this.trackSet
    }
  },
  mounted() {
    emitter.subscribe('showTrack',()=>{
      const {
        summary:{
          state
        },
        affair: {
          state:affairState,
        }
      }=this.businessData;
      //不是运行中的流程不能跟踪 （已发已办的跟踪）
      if ((affairState === 2 || affairState === 4) && state !== 0) {
        this.disable = true;
      }
      this.showModal();
      this.preTack = {
        ...this.trackSet
      }
    });
  },
  methods: {
    showModal() {
      this.visible = true;
    },
    getCurrentTrackType(){
      //0为不跟踪  1是全部人 2是指定人
      const {isTrack, isTrackAll} = this.trackSet;
      let trackType = 0;
      if (isTrack) {
        trackType = isTrackAll ? 1 : 2
      }
      return trackType;
    },
    //数据还原
    revert() {
      Object.assign(this.trackSet, this.preTack);
      if (this.trackSet.isTrackAll || !this.trackSet.isTrack) {
        this.trackSet.trackMembers = '';
        this.trackSet.trackMemberNames = '';
      }
      console.log('revert',this.trackSet);
    },
    handleOk(e) {
      const {
        affair: {
          state,
          id: affairId,
          senderId,
          objectId
        }
      } = this.businessData;
      //已发已办的跟踪
      //0为不跟踪  1是全部人 2是指定人
      const {trackMembers, isTrack, isTrackAll} = this.trackSet;
      //选了跟踪，并且不是全部，需要提示选择指定人

      if(isTrack && !isTrackAll && !trackMembers) {
        const warningMessage = this.$i18n("common.pleaseSelect.label") + this.$i18n("common.pending.designated.personnel");
        this.$message.warning(warningMessage);
        return;
      }
      if (this.trackSet.isTrackAll || !this.trackSet.isTrack) {
        this.trackSet.trackMembers = '';
        this.trackSet.trackMemberNames = '';
      }
      Object.assign(this.preTack, this.trackSet);
      this.visible = false;
      if (state === 4 || state === 2) {
        window.callBackendMethod('colManager', 'getTrackInfo', {
          affairId,
          objectId,
          senderId,
          oldTrackType: this.oldTrackType,
          newTrackType: this.getCurrentTrackType(),
          trackMemberIds: trackMembers.replace(/Member\|/g, ''),
        });
      }
    },
  },
}
</script>